<template>
  <div
    style="text-align:center;border:1px solid #C0C0C0; border-radius: 4px; width: 500px; height: 300px; background-color: #d6eff7;margin:0px auto"
  >
    <h1>登录</h1>

    <el-form :model="user" :rules="rules" ref="user" label-width="180px">
      <el-form-item label="用户名/手机号码" prop="ID">
        <el-input v-model="user.ID"></el-input>
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="user.password"></el-input>
      </el-form-item>

      <el-button type="primary" plain v-on:click="change()">注册</el-button>
      <el-button v-on:click="resetForm('user')">重置</el-button>
      <el-button type="primary" v-on:click="submitForm('user')">登录</el-button>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      user: {
        ID: "",
        password: "",
      },
      rules: {
        ID: [
          { required: true, message: "请输入用户名/手机号码", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    change() {
      this.$emit("toregister")
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var url = process.env.URL_HEAD + "/touser/user/user/login";

          this.$http
            .get(url, {
              params: { ID: this.user.ID, password: this.user.password },
              withCredentials: true,
            })
            .then(
              (response) => {
                if (response.data.status == "ok") {
                  console.log("登录成功！");
                  this.$emit("loginsuccess");
                  location.reload();
                } else {
                  this.$message.error("登录失败，请检查账号和密码");
                }
              },
              (response) => {
                this.$message.error("系统异常，请重试");
              }
            );
        } else {
          this.$message.error("请输入账号和密码");
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
.h1 {
  font-size: 50px;
}
.el-input {
  width: 200px;
}
</style>
